// map layer
var g_mapLayer = null;
var MapLayer = cc.TMXTiledMap.extend({
    ////////////////////////////
    ctor:function (tmxFile,resourcePath) {
        this._super(tmxFile,resourcePath);
        g_mapLayer=this;
        return true;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._super();
    },
    //////////////////////////////////////////////
    _getObjectGroupByName:function(name){
        var group = this.getObjectGroup(name);
        if(group){
            cc.log("get group down ");
            return group;
        }else{
            cc.log("group is null,name: "+name);
            return null;
        }
    },
    getObjectsByName:function(name){
        var group = this._getObjectGroupByName(name);
        var array = group.getObjects();
        if(array){
            cc.log("get array down ");
            return array;
        }else{
            cc.log("array is null,name: "+name);
            return null;
        }
    },
    //////////////////////////////////////////////
    addTile:function(gid,position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer) {
            var point = this.pixelToPoint(position), mapSize = this.getMapSize();
            if (point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height) {
                cc.log("over border");
                return;
            }
            var tGID = mapLayer.getTileGIDAt(point);
            cc.log("tile gid: "+tGID);
            if( tGID===0&&0!==gid ){
                mapLayer.setTileGID(gid,point);
                cc.log( "add tile at x: "+point.x+" : "+point.y );
            }else{
                cc.log( "there is other tile at x: "+point.x+" : "+point.y );
            }
        }else{
            cc.log( "mapLayer invalid from addTile()" );
            return;
        }
    },
    removeTile:function(position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer) {
            var point = this.pixelToPoint(position), mapSize = this.getMapSize();
            if (point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height) {
                cc.log("over border");
                return false;
            }
            mapLayer.removeTileAt(point);
            cc.log( "remove tile at x: "+point.x+" : "+point.y );
            return true;
        }else{
            cc.log( "mapLayer invalid from removeTile()" );
            return false;
        }
    },
    removeTileSelf:function(tile){
        if( tile ){
            tile.removeFromParent(true);
            cc.log( "remove tile " );
        }
    },
    alterTile : function(gid,position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var point = this.pixelToPoint(position),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return null;
            }
            var tGID = mapLayer.getTileGIDAt(point);
            cc.log("tile gid: "+tGID);
            if( tGID!==0 && tGID!==gid ){
                cc.log("alter tile to:" +gid+ " at x:"+point.x+" : "+point.y)
                mapLayer.setTileGID(gid,point);
            }else{
                cc.log("there is no tile or tile is already here "+gid)
            }
        }else{
            cc.log( "mapLayer invalid from alterTile()" );
            return false;
        }
    },
    /////////////////////////////////////////////
    getTile : function(position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var point = this.pixelToPoint(position),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return null;
            }
            var tile = mapLayer.getTileAt(point);
            return tile;
        }else{
            cc.log( "mapLayer invalid from getTileProperty()" );
            return null;
        }
    },
    getTileProperty:function(position,name){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var point = this.pixelToPoint(position),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return null;
            }
            var tGID = mapLayer.getTileGIDAt(point);
            cc.log("tile gid: "+tGID);
            if( tGID!==0 ){
                var dict = this.getPropertiesForGID(tGID),property = null;
                if(dict){
                    property = dict[name];
                    cc.log("tile property: "+name+" : "+property);
                }
                return property;
            }
            return null;
        }else{
            cc.log( "mapLayer invalid from getTileProperty()" );
            return null;
        }
    },
    //////////////////////////////////////////////
    collideRect:function (x, y) {
        var width=70,height=70;
        return cc.rect(x-width*0.5, y-height*0.5, width, height);
    },
    collideWithTile:function(target){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var tPos = target.getPosition();
            var point = this.pixelToPoint(tPos),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return false;
            }
            var tile = mapLayer.getTileAt(point);
            if(tile&&tile.isVisible()){
                var pixel = this.pointToPixel(point);
                if ( cc.rectContainsPoint( this.collideRect(tPos.x,tPos.y),pixel ) ){ // target.collideRect(tPos.x,tPos.y)
                    //this.removeTileSelf(tile);
                    //mapLayer.removeTileAt(point);
                    tile.setVisible(false);
                    if( 0==target.getType() ){
                        // add gold
                        AddUIMoney( 5 );
                    }
                    PlayThingEffect(0,pixel);
                    return true;
                }
            }
            return false;
        }else{
            cc.log( "mapLayer invalid from collideWithTile()" );
            return false;
        }
    },
    //////////////////////////////////////////////
    rectInTileRect : function(rect,position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var point = this.pixelToPoint(position),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return false;
            }
            var tile = mapLayer.getTileAt(point);
            if(tile&&tile.isVisible()){
                var pixel = this.pointToPixel(point);
                 var tRect = this.collideRect(pixel.x,pixel.y);
                if ( cc.rectIntersectsRect( rect,tRect ) ){
                     return true;
                 }
            }
            return false;
        }else{
            cc.log( "mapLayer invalid from rectInTileRect()" );
            return false;
        }
    },
    positionInTileRect : function(position){
        var mapLayer=this.getLayer("thing");
        if(mapLayer){
            var point = this.pixelToPoint(position),mapSize = this.getMapSize();
            if( point.x < 0 || point.x >= mapSize.width ||
                point.y < 0 || point.y >= mapSize.height  ) {
                cc.log( "over border" );
                return true;
            }
            var tile = mapLayer.getTileAt(point);
            if(tile&&tile.isVisible()){
                /*var pixel = this.pointToPixel(point);
                var tRect = this.collideRect(pixel.x,pixel.y);
                if( cc.rectContainsPoint( tRect,position )  ){
                    return true;
                }
                return false;//*/
                return true;
            }
            return false;
        }else{
            cc.log( "mapLayer invalid from getTileRect()" );
            return true;
        }
    },
    //////////////////////////////////////////////

    pixelToPoint:function(pixel){
        var tileSize=this.getTileSize();
        var tileWidth = tileSize.width;
        var tileHeight = tileSize.height;
        var winHeight = cc.winSize.height;
        //cc.log("pixel x: "+pixel.x+" y: "+pixel.y);
        var point = cc.p( Math.floor( pixel.x/tileWidth),Math.floor( (winHeight-pixel.y)/tileHeight) );
        //cc.log("pixel to point x: "+point.x+" y: "+point.y);
        return point;
    },
    pointToPixel:function(point){
        var tileSize=this.getTileSize();
        var tileWidth = tileSize.width;
        var tileHeight = tileSize.height;
        var winHeight = cc.winSize.height;
        //cc.log("point x: "+point.x+" y: "+point.y);
        var pixel = cc.p( (point.x+0.5)*tileWidth,(point.y+0.5)*tileHeight );
        pixel.y = winHeight - pixel.y;
        //cc.log("point to pixel x: "+pixel.x+" y: "+pixel.y);
        return pixel;
    }
});
///////////////////////////////////////
GetMapLayer=function(){
    if(g_mapLayer) {
        return g_mapLayer;
    }else{
        cc.log("g_mapLayer invalid");
        return null;
    }
}
GetTankAmount=function(){
    var mapLayer = GetMapLayer();
    var amount = mapLayer.getProperty("tanks");
    return amount;
}
///////////////////////////////////////
Blocked=function(x,y,direct){
    var mapLayer = g_mapLayer;
    if(mapLayer){
        var tileSize=mapLayer.getTileSize();
        var tileWidth = tileSize.width, tileHeight = tileSize.height;
        var xOff = tileWidth*(direct==1?0.5:(direct==3?-0.5:0));
        var yOff = tileHeight*(direct==0?0.5:(direct==2?-0.5:0));
        var pixelM = cc.p( x+xOff,y+yOff),scale=0.7;
        var pixelL = cc.p( x+xOff-yOff*scale,y+yOff-xOff*scale);
        var pixelR = cc.p( x+xOff+yOff*scale,y+yOff+xOff*scale );
        var rtM = mapLayer.positionInTileRect(pixelM);
        var rtL = mapLayer.positionInTileRect(pixelL);
        var rtR = mapLayer.positionInTileRect(pixelR);
        if( rtM || rtL || rtR ){
            return true;
        }
        return false;
    }else{
        cc.log( "mapLayer invalid" );
        return true;
    }
}
///////////////////////////////////////
Collide=function(target){
    var mapLayer = GetMapLayer();
    return mapLayer.collideWithTile(target);
}
